package com.mikepenz.presenter.imp;

import android.app.Activity;
import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.mikepenz.app.R;
import com.mikepenz.bean.CardBean;
import com.mikepenz.interactor.ScanInteractor;
import com.mikepenz.interactor.imp.ScanInteractorImp;
import com.mikepenz.presenter.ScanPresenter;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.CookicUntil;
import com.mikepenz.view.ScanView;

/**
 * Created by Administrator on 2015/10/9.
 */
public class ScanPresenterImp implements ScanPresenter,RequestListener<JSONObject> {

    ScanInteractor interactor;
    ScanView view;
    Context c;
    Activity act;
    String cardId;

    public ScanPresenterImp(Context c){
        this.c = c;
        this.act = (Activity) c;
        this.view = (ScanView) c;
        interactor =new ScanInteractorImp(c,this);
    }

    @Override
    public void checkCard(String cardId) {
        if(cardId == null ||cardId.length() == 0) {
            view.showError("卡片id不合法");
            return;
        }
        String userId = CookicUntil.getUser().getUserId();
        this.cardId = cardId;
        interactor.bindCard(userId,cardId);
        view.showLoad();
    }

    @Override
    public void Success(String tag, JSONObject result) {
        view.dismissLoad();
        view.showError("绑定成功，等待审核");
        CardBean bean = JSON.parseObject(result.toJSONString(),CardBean.class);
        CookicUntil.setCardBean(bean);
        view.refreshDate(bean);
//        act.finish();
    }

    @Override
    public void Error(String tag, String error) {
        view.dismissLoad();
        view.showError(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dismissLoad();
        view.showError(c.getString(R.string.system_error));
    }
}
